package com.sky.mapper;

import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 向订单表中插入数据
     * @param orders
     */
    void insertOrder(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 通过用户Id查询订单数据
     * @return
     */
    List<Orders> queryOrderByuserId(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 处理超时未支付订单
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> queryOrderByStatusAndTime(Integer status, LocalDateTime time);

    /**
     *根据订单id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id};")
    Orders queryOrderByNumber(Long id);

    /**
     * 查询营业额
     * @param map
     * @return
     */
    @Select("select sum(amount) from orders where order_time > #{begin} and order_time < #{end} and status = #{status}")
    Double getTurnoverStatistics(Map map);

    /**
     * 查询每天的新用户·
     * @param map
     * @return
     */
    @Select("select count(id) from user where create_time < #{end} and create_time > #{begin};")
    Integer getDayNewUser(Map map);

    /**
     * 查询截止到每天总的用户数量
     * @param map
     * @return
     */
    @Select("select count(id) from user where create_time < #{end};")
    Integer getTotalUser(Map map);

    /**
     * 获取每日订单数
     * @param map
     * @return
     */
    Integer getDayOrders(Map map);

    /**
     * 根据订单id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getOrderByOrderId(Long id);

    /**
     * 查询销量排名前十的商品
     * @param map
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(Map map);


    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);
}
